package com.liuweiheng.base.entity;

import io.swagger.annotations.ApiModel;

import java.util.HashMap;

/**
 * 统一接口返回数据结构
 *
 * @author lwh
 */
public class R extends HashMap<String, Object> implements IR {

    private static final long serialVersionUID = 1L;

    public R(){
        put(CODE, SUCCESS_CODE);
    }

    public static R ok() {
        return new R();
    }

    public static R ok(String message) {
        R r = new R();
        r.put(MESSAGE, message);
        return r;
    }

    public static R ok(Integer code, String message) {
        R r = new R();
        r.put(CODE, code);
        r.put(MESSAGE, message);
        return r;
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R putData(Object data) {
        super.put(DATA, data);
        return this;
    }


    public static R error(Integer code, String message) {
        R r = new R();
        r.put(CODE, code);
        r.put(MESSAGE, message);
        return r;
    }

    public static R error() {
        return error("未知异常，请联系管理员");
    }

    public static R error(String message) {
        return error(ERROR_CODE, message);
    }

    public boolean hasError() {
        return !get(CODE).equals(SUCCESS_CODE);
    }
}
